package com.comment.service.impl;

import com.comment.domain.Comments;
import com.comment.domain.Goods;
import com.comment.domain.Result;
import com.comment.mapper.CommentsMapper;
import com.comment.mapper.GoodsMapper;
import com.comment.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CommentsMapper commentsMapper;
    @Override
    public Result findAllGoodsAndComments() {
        Result result = new Result();
        List<Goods> goodsList = goodsMapper.selectAllGoodsAndComments();
        result.setData(goodsList);
        return result;
    }

    @Override
    public Result findOne(Integer gId) {
        Result result = new Result();
        List<Comments> comments= commentsMapper.selectOne(gId);
        result.setData(comments);
        return result;
    }

    @Override
    public Result findName(Integer gId) {
        Result result = new Result();

        String name= goodsMapper.selectName(gId);
        result.setData(name);

        return result;
    }

    @Override
    public Result addComment(Map map) {
        Comments comments = new Comments();
        String star=null;
        star = (String) map.get("star");
        String goodId = (String) map.get("goodId");
        String comment = (String) map.get("comment");
        Integer gid=goodsMapper.selectIdbyName(goodId);
        comments.setStar(star);
        comments.setGoodId(gid);
        comments.setComment(comment);
        Result result = new Result();
        int i=   commentsMapper.insertComment(comments);
        if (i==1){
            result.setMsg("评论成功");
        }
        return result;
    }
}
